package com.liuboya.service.tablebiz.wbuser.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liuboya.dao.weibo.wbuser.dao.WbUserDao;
import com.liuboya.dao.weibo.wbuser.entity.WbUser;
import com.liuboya.dao.weibo.wbuser.entity.WbUserExample;
import com.liuboya.dao.weibo.wbuser.entity.WbUserExtra;
import com.liuboya.service.tablebiz.wbuser.WbUserService;
import com.liuboya.weibomodel.model.enums.DeleteEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 描述：
 *
 * @author yeyuan
 * @date 2021/08/27 16:15
 **/
@Service
public class WbUserServiceImpl implements WbUserService {
    @Resource
    private WbUserDao wbuserDao;

    @Override
    public Long insert(WbUser wbuser) {
        return wbuserDao.insert(wbuser);
    }

    @Override
    public List<WbUser> find(WbUserExample example) {
        return wbuserDao.find(example);
    }

    @Override
    public Integer update(WbUser wbuser, WbUserExample example) {
        return wbuserDao.update(wbuser, example);
    }

    @Override
    public Integer count(WbUserExample example) {
        return wbuserDao.count(example);
    }

    @Override
    public WbUser findByWeiBoId(Long id) {
        WbUserExample example = new WbUserExample();
        example.createCriteria().andWeiboIdEqualTo(id)
                .andIsDeletedEqualTo(DeleteEnum.DELETED_NOT.getVal());
        List<WbUser> wbUsers = this.find(example);
        return wbUsers.isEmpty() ? null : wbUsers.get(0);
    }

    @Override
    public List<WbUser> feedJsonToUser(JSONObject jsonObject) {
        List<WbUser> result = new ArrayList<>();
        if (Objects.isNull(jsonObject)) {
            return result;
        }
        JSONArray statuses = jsonObject.getJSONArray("statuses");
        if (statuses.isEmpty()) {
            return result;
        }
        for (Object status : statuses) {
            JSONObject data = (JSONObject) status;
            JSONObject user = data.getJSONObject("user");
            WbUser wbUser = feedSingleToWbUser(user);
            result.add(wbUser);
        }
        return result;
    }

    /**
     * feed流json单个对象转换
     *
     * @param user feed流json
     * @return 实体类
     */
    WbUser feedSingleToWbUser(JSONObject user) {
        if (Objects.isNull(user)) {
            return null;
        }
        WbUser wbUser = new WbUser();
        wbUser.setName(user.getString("name"));
        wbUser.setWeiboId(user.getLong("id"));
        wbUser.setDescr(user.getString("description"));
        wbUser.setVerified(user.getString("verified_reason"));
        wbUser.setAvatar(user.getString("avatar_hd"));
        wbUser.setFollowersCount(user.getInteger("followers_count"));
        wbUser.setBlogCount(user.getInteger("statuses_count"));
        wbUser.setBlogVideoCount(user.getInteger("video_status_count"));
        JSONObject status_total_counter = user.getJSONObject("status_total_counter");
        if (Objects.nonNull(status_total_counter)) {
            Integer repostCnt = status_total_counter.getInteger("repost_cnt");
            wbUser.setRepostCount(repostCnt);
            Integer commentCnt = status_total_counter.getInteger("comment_cnt");
            wbUser.setCommentCount(commentCnt);
            Integer likeCnt = status_total_counter.getInteger("like_cnt");
            wbUser.setLikeCount(likeCnt);
            Integer commentLikeCnt = status_total_counter.getInteger("comment_like_cnt");
            wbUser.setCommentLikeCount(commentLikeCnt);
            Integer totalCnt = status_total_counter.getInteger("total_cnt");
            if (Objects.nonNull(totalCnt) && totalCnt.equals(0)) {
                wbUser.setTotalCount(repostCnt + commentCnt + likeCnt + commentLikeCnt);
            } else {
                wbUser.setTotalCount(status_total_counter.getInteger("total_cnt"));
            }
        }
        wbUser.setVideoPlayCount(user.getInteger("video_play_count"));
        wbUser.setAbility(user.getInteger("user_ability"));
        wbUser.setFriendsCount(user.getInteger("friends_count"));
        //创建时间暂存在备用字段里
        WbUserExtra wbUserExtra = new WbUserExtra();
        String createdAt = user.getString("created_at");
        wbUserExtra.setCreatedTimeString(createdAt);
        wbUser.setExtra(JSONObject.toJSONString(wbUserExtra));
        wbUser.setLocation(user.getString("location"));
        return wbUser;
    }
}